package zj.com.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zfdang.multiple_images_selector.SelectorSettings;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.UploadImgGridAdapter;
import zj.com.app.ZjApplication;
import zj.com.model.Dic;
import zj.com.model.InspectContent;
import zj.com.model.InspectItem;
import zj.com.util.Constants;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/15.
 * 检查单 详情编辑
 */
public class JcdDetailEditActivity extends  BaseActivity implements View.OnClickListener {
    public ImageView left_iv;
    public TextView title_name,right_tv,fen_tv,yh_tv;
    public EditText des_et;
    public Intent intent;

    public GridView uploadGridView;
    public List<String> list;
    public UploadImgGridAdapter adapter;
    public static final int REQUEST_CODE = 732;



    public  static ArrayList<String> mResults = new ArrayList<>();
    public InspectContent inspectContent;
    public int position = 0;
    public int subPosition = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jcd_detail_edit);
        initView();
    }

    public void initView(){

        des_et = (EditText) findViewById(R.id.des_et);

        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setVisibility(View.VISIBLE);
        left_iv.setOnClickListener(this);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("填写详情");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("保存");
        right_tv.setOnClickListener(this);

        fen_tv = (TextView) findViewById(R.id.fen_tv);
        fen_tv.setOnClickListener(this);
        yh_tv =  (TextView) findViewById(R.id.yh_tv);
        yh_tv.setOnClickListener(this);

        uploadGridView = (GridView) findViewById(R.id.uploadGridView);

        list = new ArrayList<>();
        list.add("");
        adapter = new UploadImgGridAdapter(list,this);
        uploadGridView.setAdapter(adapter);

        //inspectContent = (InspectContent) getIntent().getSerializableExtra("inspectContent");

        subPosition =   getIntent().getIntExtra("subPosition", 0);
        position = getIntent().getIntExtra("position", position);


        des_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if(inspectContent != null){

                    inspectContent.setDes(s.toString());
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        List<InspectItem> items = ZjApplication.inspectItems;
        inspectContent = items.get(position).getContents().get(subPosition);


        fen = inspectContent.getPoints();
        fen_tv.setText("扣分: "+inspectContent.getPoints());

        des_et.setText(inspectContent.getDes());

        if(inspectContent.getDic() != null ){
            dic = inspectContent.getDic();
            if(inspectContent.getDic().getName() != null){
                yh_tv.setText("隐患级别: " + inspectContent.getDic().getName());

            }else{
                yh_tv.setText("隐患级别: ");

            }

        }else {

            if(dic != null){
                yh_tv.setText("隐患级别: " +dic.getName());

            }else{
                yh_tv.setText("隐患级别: ");

            }

        }


        if(inspectContent.getImgs() != null && inspectContent.getImgs().size() > 0){

            LogUtils.getInstance().error("=========imgs========");
            list=  inspectContent.getImgs();
            //判断最后一条是否为空
            if(list.get(list.size() -1) !=""){
                list.add("");
            }
            adapter.setDataChange(list);

        }
    }

    public int fen = 0;
    public  Dic dic;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // get selected images from selector

        if(resultCode == RESULT_OK) {


            if(requestCode == FEN){
                fen = data.getIntExtra("fen",0);
                inspectContent.setPoints(fen);
                LogUtils.getInstance().error("fen=====" + fen);
                fen_tv.setText("扣分:"+fen+"分");
                return;
            }else if(requestCode == YH){
                dic = (Dic) data.getSerializableExtra("dic");
                inspectContent.setDic(dic);
                yh_tv.setText("隐患级别:"+dic.getName());
                LogUtils.getInstance().error("yh"+dic.getName());
                return;
            }

        }

        if(requestCode == REQUEST_CODE) {
            if(resultCode == RESULT_OK) {

                mResults = data.getStringArrayListExtra(SelectorSettings.SELECTOR_RESULTS);
                assert mResults != null;

                // show results in textview
                StringBuilder sb = new StringBuilder();
                sb.append(String.format("Totally %d images selected:", mResults.size())).append("\n");
                list.clear();
                for(String result : mResults) {
                    list.add(result);
                    sb.append(result).append("\n");

                }
                Log.e("wxf",sb.toString());

                list.add("");
                inspectContent.setImgs(list);
                if(inspectContent == null){
                    LogUtils.getInstance().error("inspectContent is null");
                }
                adapter.setDataChange(list);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    public final int FEN = 0x2210;
    public final int YH = FEN+1;
    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.fen_tv:
                if(inspectContent != null && inspectContent.getMaxPoints() > 0){
                    intent = new Intent(this,SelectFenActivity.class);
                    intent.putExtra("point",inspectContent.getMaxPoints());
                    startActivityForResult(intent,FEN);
                }

                break;
            case R.id.yh_tv:
                intent = new Intent(this,SelectJcdActivity.class);
                intent.putExtra("dicType", Constants.RiskLevel);
                startActivityForResult(intent, YH);
                break;
            case R.id.right_tv:


                saveData();
                break;
        }
    }

    public void saveData(){
        String des = des_et.getText().toString();

//        if(fen == -1 || fen == 0){
//            finish();
//            return;
//        }
//
//        if(dic == null){
//            Toast.makeText(this,"请选择隐患类别",Toast.LENGTH_SHORT).show();
//            return;
//        }
//
//        if(TextUtils.isEmpty(des)){
//
//            Toast.makeText(this,"请写点描述",Toast.LENGTH_SHORT).show();
//            return;
//        }
//        if(list.size() == 1){
//            Toast.makeText(this,"请上传点图片",Toast.LENGTH_SHORT).show();
//            return;
//        }
//
//        if(fen == 0){
//            Toast.makeText(this,"不允许扣0分",Toast.LENGTH_SHORT).show();
//            return;
//        }

        inspectContent.setDes(des);
        inspectContent.setImgs(list);
        inspectContent.setDic(dic);
        inspectContent.setPoints(fen);
        finish();


    }
}
